extends Control
class_name bag
@export var bag_space: ItemContainer = ItemContainer.new() 
@export var slot_scene: PackedScene
@export var mouse_slot: Control
@export var bag_columns: int = 3
var bag_name = "Bag" #背包名
@onready var bag_grid_container = $MarginContainer/ScrollContainer/GridContainer
var is_first: bool = true

func _ready():
	bag_grid_container.columns = bag_columns
	EventBus.subscribe("refresh_slots", on_refresh_slots)
	set_columns()
	init_slot()
	init_bag()
	pass

## 刷新slot节点
func on_refresh_slots():
	pass

func bag_space_add_item(item_stats: PandoraEntity) -> bool:
	if not bag_space.add_item(item_stats.get_entity_id()):
		return false
	var _index = 0
	for item_info in bag_space.items_in_container_stuck:
		if item_info != null:
			var slot = bag_grid_container.get_child(item_info["index"])
			slot.item_info_dict = item_info
			slot.item_pandora = Pandora.get_entity(slot.item_info_dict["id"]).instantiate()
		_index += 1
	return true

## 合成
func synthesis(item_stats: PandoraEntity) -> bool:
	var index = 0
	for item_info in bag_space.items_in_container_stuck:
		var slot = bag_grid_container.get_child(index)
		if not slot.item_info_dict.is_empty():
			var dict = {}
			dict.get_or_add(slot.item_info_dict["id"])
			dict.get_or_add(item_stats.get_entity_id())
			var recipe = ItemManager.recipe_book.find_recipe(dict)
			if recipe != null:
				var target = Pandora.get_entity(recipe.target_id)
				EventBus.push_event("add_item", [target])
				slot.item_pandora = null
				var slot_index = slot.item_info_dict["index"]
				slot.item_info_dict = {}
				bag_space.items_in_container_stuck[slot_index] = null
				return true
		index += 1
	return false
	
func bag_space_remove_item(id: String, amount: int):
	bag_space.remove_item(id, amount)
	init_bag()

## 实例化里面的slot节点
func init_slot():
	for x in range(bag_space.max_slot_count):
		var c = slot_scene.instantiate()
		c.bag_name = bag_name
		bag_grid_container.add_child(c)
		c.mouse_slot = mouse_slot

## 先加一堆空节点
func init_bag():
	for x in range(bag_space.max_slot_count):	
		bag_space.items_in_container_stuck.append(null)
	var index = 0
	for child in bag_grid_container.get_children():
		child.index = index
		child.bag_tscn = self
		index += 1
	
func find_null_slot(): #查找空的格子
	var null_item_info_array: Array
	for c in bag_grid_container.get_children():			
		if not c.item_info:
			null_item_info_array.append(c)
	return null_item_info_array
	
func _exit_tree():
	save_bag()
	pass

func save_bag():
	var item_in_bag: Array = []
	for slot in bag_grid_container.get_children():
		if slot.item_info_dict and slot.item_info_dict.has("id"):
			item_in_bag.append(slot.item_info_dict["id"])
			# 将每个非空格子保存到资源文件
	bag_space.items_in_container_stuck = item_in_bag
	bag_space.save_as_resource(bag_space.resource_path)
	# 将bag资源更新
	print(bag_space.items_in_container_stuck)
	pass

func set_columns():
	if bag_columns >= 10:
		size.x = 800
		return
	size.x = bag_columns * 80

func close_bag():
	if mouse_slot.item_info_dict:
		var item_info: Dictionary = mouse_slot.item_info_dict.duplicate()
		mouse_slot.clear_mouse_slot()
		bag_space.add_item(item_info["id"])
		init_bag()
		
	visible = false
	
func open_bag():
	visible = true

func _on_open_button_down():
	open_bag()	
	pass # Replace with function body.
